package com.haiyi.software.app;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.google.gson.JsonObject;
import com.haiyi.software.app.basic.ActivityBasicRegisterUsername;
import com.haiyi.software.entity.UserInfo;

import android.R.bool;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

@SuppressLint("ResourceAsColor")
public class RegisterUsername extends ActivityBasicRegisterUsername implements
		OnClickListener {

	// 下一步按钮
	Button nextBtn;

	// 用户名文本框
	EditText userNameText;

	// 协议
	TextView textView_protocol;

	EditText passwordText, passwordAgainText;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register_username);
		initComp();
	}

	public void initComp() {

		nextBtn = (Button) findViewById(R.id.nextBtn_username);

		userNameText = (EditText) this.findViewById(R.id.login_name);

		textView_protocol = (TextView) this
				.findViewById(R.id.textView_protocol);

		passwordText = (EditText) this.findViewById(R.id.input_pass_editText);

		passwordAgainText = (EditText) this
				.findViewById(R.id.input_pass_again_editText);

		textView_protocol.setOnClickListener(this);
		nextBtn.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.textView_protocol:
			Intent intent = new Intent();
			intent.setClass(this, ActivityProtocol.class);
			startActivity(intent);
			break;
		case R.id.nextBtn_username:
			Log.i("ckname", "ckname");
			if (ckeckUserName()) {

				switch (checkPassword()) {
				case 0:
					Toast.makeText(v.getContext(), "请填写登陆密码",
							Toast.LENGTH_SHORT).show();
					break;
				case -1:
					Toast.makeText(v.getContext(), "再次密码不一致",
							Toast.LENGTH_SHORT).show();
					break;
				case 1:
					String uname = userNameText.getText().toString().trim();
					String pwd = passwordText.getText().toString().trim();
					watingDialog.show();
					requestNetworkData(uname, pwd);
					break;
				}

			} else {
				Toast.makeText(v.getContext(), "电子邮箱不合法 ", Toast.LENGTH_SHORT)
						.show();
			}
			break;
		}
	}

	private boolean ckeckUserName() {

		// TODO 自动生成的方法存根
		Pattern p = Pattern
				.compile("^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$");
		Matcher m = p.matcher(userNameText.getText().toString().trim());

		return m.matches();
	}

	private int checkPassword() {

		String pwd = passwordText.getText().toString().trim();
		String pwdAgain = passwordAgainText.getText().toString().trim();

		if (pwd == null || pwd.length() <= 0) {
			return 0;
		} else {
			if (pwd.equals(pwdAgain)) {
				return 1;
			} else {
				return -1;
			}
		}
	}

	@Override
	protected void requestFinish(JsonObject jsonObjectReturn) {

		watingDialog.hide();

		try {

			JsonObject jsonReturn = jsonObjectReturn;

			Toast.makeText(MainApplication.getInstance(),
					jsonReturn.get("desc").getAsString(), Toast.LENGTH_LONG)
					.show();

			if (jsonReturn.get("state").getAsString().equals("200")) {

				JsonObject jsonBody = jsonReturn.getAsJsonObject("body");

				Intent intent = new Intent();

				intent.putExtra("uid", jsonBody.get("uid").getAsString());
				intent.putExtra("uname", jsonBody.get("uname").getAsString());
				intent.putExtra("pwd", jsonBody.get("pwd").getAsString());
				intent.putExtra("uguid", jsonBody.get("uguid").getAsString());

				intent.setClass(this, RegisterSex.class);

				startActivity(intent);
			}

		} catch (Exception e) {

		}

	}

	@Override
	protected void requestError() {
		// TODO 自动生成的方法存根

	}
}
